/**
 * Package:com.goldcitynet.util;
 * $Id: ToHtml.java,v 1.1.1.1 2009/03/23 09:21:18 zhangpei Exp $
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.util;

import sun.io.Converters;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * ToHtml
 * <p/>
 * <p><a href="ToHtml.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: zhangpei $</a>
 * @version $Revision: 1.1.1.1 $
 */
public class ToHtml {
    private static Log log = LogFactory.getLog(ToHtml.class);


    /**
     * 将信息转化为静态html
     * @param sSourceUrl 动态信息访问URL
     * @param sDestDir  存储为静态文件的目录
     * @param sHtmlFile 生成的静态文件名,可以按信息的唯一ID+.html命名
     * @throws IOException
     */
    public static void convert2Html(String sSourceUrl,String sDestDir,String sHtmlFile) throws IOException
    {
          int HttpResult;
           URL url=new URL(sSourceUrl);
           URLConnection urlconn=url.openConnection();
           urlconn.connect();
           HttpURLConnection httpconn=(HttpURLConnection)urlconn;
           HttpResult=httpconn.getResponseCode();
           if(HttpResult==HttpURLConnection.HTTP_OK) {
               InputStreamReader isr =
             new InputStreamReader(httpconn.getInputStream());
             BufferedReader in = new BufferedReader(isr);

             String inputLine;
             if(!sDestDir.endsWith("/"))sDestDir+="/";

             File dir = new File(sDestDir);
             if(!dir.isDirectory()) dir.mkdir();
             FileOutputStream fout = new FileOutputStream(sDestDir+sHtmlFile);
               String csn = Converters.getDefaultEncodingName();
               log.debug("csn:" + csn);

               while ((inputLine = in.readLine()) != null)
             {

              fout.write(inputLine.getBytes());

             }
             in.close();
             fout.close();
           }
    }



    public static void main(String[] args) throws IOException
    {

         ToHtml.convert2Html("http://localhost:8080/goldcity/afteryuan/index.action","D://www//goldcity","index.html");
         ToHtml.convert2Html("http://localhost:8080/goldcity/afteryuan/defaultPageItem.action","D://www//goldcity//itemcenter","index.html");
         ToHtml.convert2Html("http://localhost:8080/goldcity/afteryuan/personItemPage.action","D://www//goldcity//itemcenter//person","index.html");
         ToHtml.convert2Html("http://localhost:8080/goldcity/afteryuan/storeItemPage.action","D://www//goldcity//itemcenter//store","index.html");
         ToHtml.convert2Html("http://localhost:8080/goldcity/afteryuan/defaultPageMall.action","D://www//goldcity//mall","index.html");
         ToHtml.convert2Html("http://localhost:8080/goldcity/afteryuan/digital.action","D://www//goldcity//mall//digital","index.html");

    }
 
}
